package ru.dc.object.commands;

import org.apache.commons.lang.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <pre>
 * User: Penkov Vladimir
 * Date: 08.10.2010
 * Time: 17:54:25
 * </pre>
 */
public class MyINFO extends Command {

    private String interest;
    private String speed;
    private String email;
    private long sharesize;
    private String nick;

    @Override
    public String generateCommand() {
        return getActualCommandName()+" $ALL "+getNick()+" "+getInterest()+"$ $"+getSpeed()+"7$"+getEmail()+"$"+getSharesize()+"$";
    }

    @Override
    public String getActualCommandName() {
        return "$MyINFO";
    }

    @Override
    public void parseCommand() {

        String cmd = getCommand();

        int startNickIndex = 13;// length('$MyINFO $ALL')
        int endNickIndex = cmd.indexOf(" ", startNickIndex);
        setNick(cmd.substring(startNickIndex, endNickIndex));

        int startInterestIndex = endNickIndex+1;
        int endInterestIndex = cmd.indexOf("$ $", startInterestIndex);
        setInterest(cmd.substring(startInterestIndex, endInterestIndex));

        int startSpeedIndex = endInterestIndex+3;
        int endSpeedIndex = cmd.indexOf("$", startSpeedIndex);
        setSpeed(cmd.substring(startSpeedIndex, endSpeedIndex));

        int startEmailIndex = endSpeedIndex+1;
        int endEmailIndex = cmd.indexOf("$", startEmailIndex);
        setEmail(cmd.substring(startEmailIndex, endEmailIndex));

        int startShareIndex = endEmailIndex+1;
        int endShareIndex = cmd.indexOf("$", startShareIndex);
        String shareStr = cmd.substring(startShareIndex, endShareIndex);
        if (!StringUtils.isEmpty(shareStr) && StringUtils.isNumeric(shareStr)) {
            setSharesize(new Long(shareStr));
        }

    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getInterest() {
        return interest;
    }

    public void setInterest(String interest) {
        this.interest = interest;
    }

    public long getSharesize() {
        return sharesize;
    }

    public void setSharesize(long sharesize) {
        this.sharesize = sharesize;
    }

    public String getSpeed() {
        return speed;
    }

    public void setSpeed(String speed) {
        this.speed = speed;
    }

    public String getNick() {
        return nick;
    }

    public void setNick(String nick) {
        this.nick = nick;
    }
}
